package com.sq.activity.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sq.activity.domain.Activity;
import com.sq.activity.domain.ActivityPublisherApply;
import com.sq.activity.mapper.ActivityMapper;
import com.sq.activity.service.IActivityService;
import com.sq.common.core.domain.AjaxResult;
import com.sq.common.core.domain.entity.SysRole;
import com.sq.common.core.domain.entity.SysUser;
import com.sq.common.utils.DateUtils;
import com.sq.common.utils.UserThreadLocal;
import com.sq.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sq.activity.mapper.ActivityRegistrationMapper;
import com.sq.activity.domain.ActivityRegistration;
import com.sq.activity.service.IActivityRegistrationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 活动报名Service业务层处理
 * 
 * @author shan
 * @date 2025-02-12
 */
@Service
public class ActivityRegistrationServiceImpl extends ServiceImpl<ActivityRegistrationMapper, ActivityRegistration> implements IActivityRegistrationService
{
    @Autowired
    private ActivityRegistrationMapper activityRegistrationMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 查询活动报名
     * 
     * @param id 活动报名主键
     * @return 活动报名
     */
    @Override
    public ActivityRegistration selectActivityRegistrationById(Long id) {
        return activityRegistrationMapper.selectonId(id);
    }

    /**
     * 查询活动报名列表
     * 
     * @param activityRegistration 活动报名
     * @return 活动报名
     */
    @Override
    public List<ActivityRegistration> selectActivityRegistrationList(ActivityRegistration activityRegistration) {
        return activityRegistrationMapper.selectActivityRegistrationList(activityRegistration);
    }

    /**
     * 新增活动报名
     * 
     * @param activityRegistration 活动报名
     * @return 结果
     */
    @Override
    public AjaxResult insertActivityRegistration(ActivityRegistration activityRegistration) {
        //非空判断
        if (ObjectUtils.isEmpty(activityRegistration)){
            return AjaxResult.error("活动报名信息不能为空");
        }
        //获取报名用户的信息
        SysUser sysUser = sysUserMapper.selectUserByUserNickName(activityRegistration.getSysUserNickName());
        if (ObjectUtils.isEmpty(sysUser)){
            return AjaxResult.error("用户信息不存在");
        }
        //判断当前用户是否已经报名该活动
            //根据活动title查询活动信息
        Activity activity = activityMapper.getOneByTitle(activityRegistration.getActivityTitle());
        if (ObjectUtils.isEmpty(activity)){
            return AjaxResult.error("活动信息不存在");
        }
        //根据活动id查询活动报名信息
        List<ActivityRegistration> list = activityRegistrationMapper.selectByActivityId(activity.getId());
        for (ActivityRegistration registration : list) {
            if(registration.getSysUserId().equals(sysUser.getUserId())){
                return AjaxResult.error("您已经报名该活动，请勿重复报名");
            }
        }
        //判断活动状态
        if(activity.getStatus() != 1){
            return AjaxResult.error("活动已结束，不可报名");
        }
        //判断活动报名人数是否超过上限
        if(activity.getParticipants() >= activity.getMaxParticipants()){
            return AjaxResult.error("活动报名人数已满，请选择其他活动报名");
        }
        //封装数据
        activityRegistration.setActivityId(activity.getId());
        activityRegistration.setSysUserId(sysUser.getUserId());
        activityRegistration.setRegTime(LocalDateTime.now());
        int i = activityRegistrationMapper.insertActivityRegistration(activityRegistration);

        if(i>=0){
            //更新活动报名人数
            activity.setParticipants(activity.getParticipants()+1);
            activityMapper.updateActivity(activity);
            return AjaxResult.success();
        }
       return AjaxResult.error();
    }

    /**
     * 修改活动报名
     * 
     * @param activityRegistration 活动报名
     * @return 结果
     */
    @Override
    public int updateActivityRegistration(ActivityRegistration activityRegistration) {

        //判断当前用户是否有权限修改活动报名信息
        SysUser sysUser = sysUserMapper.selectUserById(UserThreadLocal.getUserId());
        List<SysRole> roles = sysUser.getRoles();
        for (SysRole role : roles) {
            if(!(role.getStatus().equals("1") && role.getRoleKey().equals("common"))){
                //非普通用户
                activityRegistration.setRegTime(LocalDateTime.now());
                return activityRegistrationMapper.updateActivityRegistration(activityRegistration);
            }
        }
        //判断是否是自己的报名信息,只有自己的报名信息可以修改
        ActivityRegistration registration = activityRegistrationMapper.selectActivityRegistrationById(activityRegistration.getId());
        Long sysUserId = registration.getSysUserId();
        if (sysUserId.equals(UserThreadLocal.getUserId())) {
            return activityRegistrationMapper.updateActivityRegistration(activityRegistration);
        }
        return 0;
    }

    /**
     * 批量删除活动报名
     * 
     * @param ids 需要删除的活动报名主键
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationByIds(Long[] ids) {
        //判断当前用户是否有权限删除活动报名信息
        SysUser sysUser = sysUserMapper.selectUserById(UserThreadLocal.getUserId());
        if (sysUser.getStatus().equals("0")) {
            List<SysRole> roles = sysUser.getRoles();
            for (SysRole role : roles) {
                if(!(role.getStatus().equals("1") && role.getRoleKey().equals("common"))){
                   return activityRegistrationMapper.deleteActivityRegistrationByIds(ids);
                }
            }
            //普通用户，判断是否是自己的报名信息
            for (Long id : ids) {
                ActivityRegistration registration = getById(id);
                Long sysUserId = registration.getSysUserId();
                if(sysUserId.equals(UserThreadLocal.getUserId())){
                    return activityRegistrationMapper.deleteActivityRegistrationById(id);

                }
            }
        }
        return 0;
    }

    /**
     * 删除活动报名信息
     * 
     * @param id 活动报名主键
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationById(Long id) {
        //判断当前用户是否有权限删除活动报名信息
        SysUser sysUser = sysUserMapper.selectUserById(UserThreadLocal.getUserId());
        if (sysUser.getStatus().equals("0")) {
            List<SysRole> roles = sysUser.getRoles();
            for (SysRole role : roles) {
                if (!(role.getStatus().equals("1") && role.getRoleKey().equals("common"))) {
                    return activityRegistrationMapper.deleteActivityRegistrationById(id);
                }
            }
            //判断是否是自己的报名信息
            ActivityRegistration registration = activityRegistrationMapper.selectActivityRegistrationById(id);
            Long sysUserId = registration.getSysUserId();
            if (sysUserId.equals(UserThreadLocal.getUserId())) {
                return activityRegistrationMapper.deleteActivityRegistrationById(id);
            }
        }
        return 0;
    }
}
